<?php
header("Content-type: text/html; charset=utf-8"); 
/**
* 
*/
class Form extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');

		// $this->form_validation->set_rules('username','Username','required');
		// $this->form_validation->set_rules('password','Password','required');
		// $this->form_validation->set_rules('passconf','Password Confirmation','required');
		// $this->form_validation->set_rules('email','Email','required');

		//定义表单验证规则
		$validation_config = array(
			array(
				'field'=>'username',
				'label'=>'Username',
					// 'rules'=>'required|min_length[5]|max_length[12]'//用户名表单域长度不得小于5个字符以及大于12个字符
					'rules'=>'callback_username_check' //验证回调函数，回调函数名称为：username_check
					),
			array(
				'field'=>'password',
				'label'=>'Password',
					'rules'=>'required|matches[passconf]' //密码表单域必须跟密码确认表单域的数据一致。
					),
			array(
				'field'=>'passconf',
				'label'=>'Password Confirmation',
				'rules'=>'required'
				),
			array(
				'field'=>'email',
				'label'=>'Email',
					'rules'=>'required|valid_email|is_unique[users.email]' //电子邮件表单域必须是一个有效邮件地址。
					)
			);
		//加载表单验证规则
		$this->form_validation->set_rules($validation_config);

		$this->form_validation->set_error_delimiters('<div class="error">','</div>');

		if ($this->form_validation->run() === FALSE) {
			$this->load->view('myform',array('form_test'=>124));
		}
		else
		{
			$this->load->view('formsuccess');
		}
	}
	/**
	 * 校验用户名（该函数为表单验证的回调函数）
	 */
	public function username_check($user_name)
	{
		if ($user_name == 'test') {
			$this->form_validation->set_message('username_check','This %s field can not be the word "test" !');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	public function test_calendar()
	{
		$this->load->library('calendar');
		$data = array(
			3 => 'http://www.cistudent.com/index.php/form/test_calendar/2006/03',
			7 => 'http://www.cistudent.com/index.php/form/test_calendar/2006/07',
			13 => 'http://www.cistudent.com/index.php/form/test_calendar/2006/13',
			26 => 'http://www.cistudent.com/index.php/form/test_calendar/2006/26' );
		echo $this->calendar->generate(2006,6,$data);
	}

	public function test_fuzhu()
	{
		echo "网站地址：". $this->config->site_url().'<br/>';
		echo "网站根地址:".$this->config->base_url().'<br/>';
		echo "System地址:".$this->config->system_url().'<br/>' ;
	}

}

#Form Class End